package cn.zebra.dev.shop.module.collect.service.impl;

import cn.zebra.dev.ibatis.plus.service.impl.ServiceImpl;
import cn.zebra.dev.ibatis.plus.utils.PageUtils;
import cn.zebra.dev.ibatis.plus.utils.Query;
import cn.zebra.dev.shop.module.collect.dao.MemberShopCollectDao;
import cn.zebra.dev.shop.module.collect.entity.MemberShopCollectEntity;
import cn.zebra.dev.shop.module.collect.service.MemberShopCollectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * @author runnable@sina.cn
 */
@Service
public class MemberShopCollectServiceImpl extends ServiceImpl<MemberShopCollectDao, MemberShopCollectEntity> implements MemberShopCollectService {
    /**
     * 保存店铺收藏
     *
     * @param memberId 用户id
     * @param shopId   店铺id
     * @return 保存成功返回True 保存失败返回False
     */
    @Override
    public Boolean saveCollect(Long memberId, Long shopId) {
        MemberShopCollectEntity entity = this.findByMemberIdAndProductId(memberId, shopId);
        if (entity == null) {
            entity = new MemberShopCollectEntity();
            entity.setDeleteFlag(false);
            entity.setCreateTime(LocalDateTime.now());
            entity.setMemberId(memberId);
            entity.setShopId(shopId);
            return save(entity);
        }
        return false;
    }

    /**
     * 根据用户id和商品id查询用户店铺收藏
     *
     * @param memberId 用户id
     * @param shopId   店铺id
     * @return 用户店铺收藏对象
     */
    @Override
    public MemberShopCollectEntity findByMemberIdAndProductId(Long memberId, Long shopId) {
        QueryWrapper<MemberShopCollectEntity> wrapper = new QueryWrapper<>();
        wrapper.apply("member_id = {0}", memberId);
        wrapper.apply("shop_id = {0}", shopId);
        wrapper.apply("delete_flag = FALSE");
        return this.getOne(wrapper);
    }

    /**
     * 根据主键id和用户id逻辑删除用户店铺收藏
     *
     * @param id       主键id
     * @param memberId 用户id
     * @return 逻辑删除成功返回True 逻辑删除失败返回False
     */
    @Override
    public Boolean deleteByIdAndMemberId(Long id, Long memberId) {
        MemberShopCollectEntity entity = new MemberShopCollectEntity();
        entity.setDeleteFlag(true);
        UpdateWrapper<MemberShopCollectEntity> wrapper = new UpdateWrapper<>();
        wrapper.apply("member_id = {0}", memberId);
        wrapper.apply("id = {0}", id);
        wrapper.apply("delete_flag = FALSE");
        return this.update(entity, wrapper);
    }

    /**
     * 根据查询参数查询分页用户店铺收藏信息
     *
     * @param params - key(查询参数)
     *               memberId   用户id
     *               shopId     店铺id
     *               deleteFlag 是否删除
     * @return 用户店铺收藏分页信息
     */
    @Override
    public PageUtils<MemberShopCollectEntity> findPage(Map<String, Object> params) {
        QueryWrapper<MemberShopCollectEntity> wrapper = new QueryWrapper<>();
        wrapper.select("id, shop_id, member_id");
        wrapper.apply(params.containsKey("memberId"), "member_id = {0}", params.get("memberId"));
        wrapper.apply(params.containsKey("shopId"), "shop_id = {0}", params.get("shopId"));
        wrapper.apply(params.containsKey("deleteFlag"), "delete_flag = {0}", params.get("deleteFlag"));
        wrapper.orderByDesc("id");
        IPage<MemberShopCollectEntity> iPage = this.page(
                new Query<MemberShopCollectEntity>(params).getPage(),
                wrapper
        );
        return new PageUtils<>(iPage);
    }
}
